/*
Petya is preparing for his birthday. He decided that there would be n different dishes on the dinner table, numbered from 1 to n. Since Petya doesn't like to cook, he wants to order these dishes in restaurants.

Unfortunately, all dishes are prepared in different restaurants and therefore Petya needs to pick up his orders from n different places. To speed up this process, he wants to order courier delivery at some restaurants. Thus, for each dish, there are two options for Petya how he can get it:

the dish will be delivered by a courier from the restaurant i, in this case the courier will arrive in ai minutes,
Petya goes to the restaurant i on his own and picks up the dish, he will spend bi minutes on this.
Each restaurant has its own couriers and they start delivering the order at the moment Petya leaves the house. In other words, all couriers work in parallel. Petya must visit all restaurants in which he has not chosen delivery, he does this co
*/
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#define ll long long
#define debug(x) cout<<#x<<": "<<x<<endl
using namespace std;
const int maxn = 2e5+10;
ll a[maxn], b[maxn];
inline bool scan_d(int &num)
{
    char in;bool IsN=false;
    in=getchar();
    if(in==EOF) return false;
    while(in!='-'&&(in<'0'||in>'9')) in=getchar();
    if(in=='-'){ IsN=true;num=0;}
    else num=in-'0';
    while(in=getchar(),in>='0'&&in<='9'){
        num*=10,num+=in-'0';
    }
    if(IsN) num=-num;
    return true;
}
inline bool scan_lf(double &num)
{
    char in;double Dec=0.1;
    bool IsN=false,IsD=false;
    in=getchar();
    if(in==EOF) return false;
    while(in!='-'&&in!='.'&&(in<'0'||in>'9'))
        in=getchar();
    if(in=='-'){IsN=true;num=0;}
    else if(in=='.'){IsD=true;num=0;}
    else num=in-'0';
    if(!IsD){
        while(in=getchar(),in>='0'&&in<='9'){
            num*=10;num+=in-'0';}
    }
    if(in!='.'){
        if(IsN) num=-num;
        return true;
    }else{
        while(in=getchar(),in>='0'&&in<='9'){
            num+=Dec*(in-'0');Dec*=0.1;
        }
    }
    if(IsN) num=-num;
    return true;
}
bool judge(int m,int n)
{
	ll ans = 0;
	for(int i=1; i<=n;i++)
	{
		if (a[i]>m) ans += b[i];
	}
	if(ans<=m) return true;
	else return false;
}
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int n;
		cin>>n;
		for (int i=1; i<=n;i++) 
		{
			cin>>a[i];
		}
		for (int i=1; i<=n;i++)
		{
			cin>>b[i];
		}
		int l=1;
		int r=0x3f3f3f3f;
		while (l<r)
		{
			int mid=(l+r)/2;
			if (judge(mid,n)) r=mid;
			else l = mid+1;
		}
		cout<<l<<endl;
	}
	return 0;
}
/*
4
4
3 7 4 5
2 1 2 4
4
1 2 3 4
3 3 3 3
2
1 2
10 10
2
10 10
1 2

*/
